<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
		
	<cffunction name="createAccount" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />
		<cfargument name="first_name" required="true">
		<cfargument name="middle_name" required="false" default="" />
		<cfargument name="last_name" required="true">
		<cfargument name="code" required="true">
		<!--- collect timezone info on login page --->
		<cfargument name="dst" required="true">
		<cfargument name="timezone" required="true">
		
		<cfset var local = {} />
		
		
		<cfset local.error = "" />
		<!--- set the email to the previously used email --->
		<cfcookie name="c63" expires="never" value="#$.format.encrypt(arguments.email)#" />
		
		<cfset local.userCount = $.run.it('craftivityService.getUserCount') />
		<cfset local.open_spots = application.userLimit - local.userCount.counts />
		
		<!--- check for existing email --->
		<cfset local.user = $.run.it('craftivityService.getUser', {email=arguments.email}) />
		<cftransaction>
			<cfif local.user.recordcount gt 0>
				<cfset local.error = "message=#arguments.email# has already been activated." />
			<cfelseif arguments.code neq 'beproductive'>
				<cfset local.error = "message=invalid beta code.">
			<cfelseif local.open_spots lte 0>
				<cfset local.error = "message=Sorry, there are no spots available." />
			<cfelse>
				<!--- create a business for the user --->
				<cfset local.businessID = $.run.it('craftivityService.saveBusiness',{
					name="unnamed business",
					stateID="",
					website="",
					description="",
					timezone=arguments.timezone,
					dst=arguments.dst,
					continent=""
				}) />
				
				<!--- create the user--->
				<cfset local.userID = $.database.save(
					table="user",
					id="",
					business_id=local.businessID,
					first_name=arguments.first_name,
					middle_name=arguments.middle_name,
					last_name=arguments.last_name,
					email=arguments.email,
					password=$.format.encrypt(arguments.password)
				) />
				
				<!--- delete the user, but keep the key for activation link --->
				<cfset $.database.delete(
					table="user",
					id=local.userID		
				) />
				
				<!--- populate hte user's stuffs --->
				<cfset $.run.it('craftivityService.populateNewAccount',{userID=local.userID}) />
			</cfif>
		</cftransaction>
		<cfif local.error eq "">
			<!--- send activation email --->
			<cfset application.s.emailService.sendCraftivityBetaEmail(email=arguments.email,userID=local.userID,first_name=arguments.first_name) />
			
			<cflocation url="#$.site.getVar('rootURL')#/beta_signup.cfm?outcome=success&#local.error#&email=#arguments.email#" addToken="no" />
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/beta_signup.cfm?outcome=failure&#local.error#&email=#arguments.email#" addToken="no" />
		</cfif>
	
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="login" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.error = "" />
		
		<cfif arguments.password eq ''>
			<cfset arguments.password = '-1' />
		</cfif>
		
		<!--- check for user/password match --->
		<cfset local.user = $.run.it('craftivityService.getUser', {email=arguments.email,password=arguments.password}) />
		<cfif local.user.recordcount gt 0>
			<cfset $.run.it('craftivityService.setLoggedInUser', {userID=local.user.id}) />
			<cfcookie name="c64" expires="never" value="#$.format.encrypt(arguments.password)#" />
			<cflocation url="#$.site.getVar('rootURL')#/dashboard.cfm#local.error#" addToken="no" />
			<!--- save password for auto login --->
		<cfelse>
			<cfset local.error = "?error=Email and/or password not found." />
			<cflocation url="#$.site.getVar('rootURL')#/login.cfm#local.error#&email=#arguments.email#" addToken="no" />
		</cfif>

	</cffunction>
	
	<!--- --->
	
	<cffunction name="logout" access="public" output="true" returntype="boolean">
		<cfargument name="message" required="yes" />

		<cfset local.id = $.database.delete(
			table="session",
			id=request.session.id
		) />
		
		<!--- unset password for autologin --->
		<cfcookie name="c64" expires="#now()#" value="" />
		<cfcookie name="c24" expires="#now()#" value="" />
		
		<cflocation url="#$.site.getVar('rootURL')#/login.cfm?message=#arguments.message#" addToken="no" />
		
		<cfreturn true />
			
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveBusiness" access="public" output="true" returntype="void">
		
		<cfset var local = {} />
		
		<cfset local.error = "" />
		
		<cfset arguments.businessID = $.run.it('craftivityService.saveBusiness', arguments) />
		<cfset $.run.it('craftivityService.saveUser', arguments) />
		
		<cflocation url="#$.site.getVar('rootURL')#/craftivity/setup.cfm#local.error#" addToken="no" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveLocationTimezone" access="public" output="true" returntype="void">
		
		<cfset $.run.it('craftivityService.saveLocationTimezone',arguments) />

		<cflocation url="#cgi.http_referer#" addToken="no" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="addSignupEmail" access="public" output="true" returntype="void">
		
		<cfset var local = {} />

		<cfset local.result = $.run.it('craftivityService.addSignupEmail',arguments) />
				
		<cfif local.result eq "">
			<cflocation url="#$.site.getVar('rootURL')#/craftivity.cfm?signup=true" addToken="no" />
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/craftivity.cfm?error=#local.result#" addToken="no" />
		</cfif>

	</cffunction>
	
	<!--- --->
	
	<cffunction name="initSite" access="public" output="true" returntype="void">
		
		<cfset var local = {} />

		<cflocation url="#$.link.setArg(cgi.http_referer,'craft')#" addToken="no" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="activateAccount" access="public" output="true" returntype="void">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.result = $.run.it('craftivityService.activateAccount',{userID=arguments.userID}) />
		
		<cfif local.result eq 1> <!--- ok! --->
			<cfset local.user = $.run.it('craftivityService.getUser',{userID=arguments.userID}) />
			<cfset $.run.it('craftivityController.login',{email=local.user.email,password=$.format.decrypt(local.user.password)}) />
		<cfelseif local.result eq 2> <!--- no such user --->
			<cflocation url="#$.site.getVar('rootURL')#/beta_signup.cfm?message=Sorry, the activation link you used is no longer valid." addToken="no" />
		<cfelse> <!--- invalid link --->
			<cflocation url="#$.site.getVar('rootURL')#/beta_signup.cfm?message=Sorry, the activation link you used is not valid." addToken="no" />
		</cfif>

	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendRecoveryEmail" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.error = "" />
		
		<!--- check for existing email --->
		<cfset local.user = $.run.it('craftivityService.getUser', {email=arguments.email}) />
		<cfif local.user.recordcount eq 0>
			<cfset local.error = "message=Sorry</br> #arguments.email# </br>isn't an email we recognize." />
		</cfif>
		
		<cfif local.error eq "" and local.user.recordcount gt 0>
			<!--- send activation email --->

			<cfset application.s.emailService.sendRecoveryEmail(email=arguments.email,userID=local.user.id,first_name=local.user.first_name) />
			
			<cflocation url="#$.site.getVar('rootURL')#/recover_password.cfm?outcome=success&#local.error#&email=#arguments.email#" addToken="no" />
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/recover_password.cfm?outcome=failure&#local.error#&email=#arguments.email#" addToken="no" />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="resetPassword" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />
		<cfargument name="activate" required="true" />
		
		<cfset var local = {} />
		
		<cfset arguments.userID = right(arguments.activate,20) & left(arguments.activate,12) />
		
		<cfset local.error = "" />
		
		<!--- check for existing user --->
		<cfset local.user = $.run.it('craftivityService.getUser', {userID=arguments.userID}) />
		<cfif local.user.recordcount eq 0 or local.user.email neq arguments.email or len(arguments.userID) neq 32>
			<cfset local.error = "message=Sorry, the email entered doesn't match your account." />
		</cfif>

		<cfif local.error eq "" and local.user.recordcount gt 0>
			<!--- reset password --->
			<cfset $.database.save(
				table="user",
				id=arguments.userID,
				password=$.format.encrypt(arguments.password)
			) />
			<cfset local.error = "message=Password has been reset" />
			<cflocation url="#$.site.getVar('rootURL')#/login.cfm?#local.error#&email=#arguments.email#" addToken="no" />
		<cfelse>
			<cflocation url="#$.site.getVar('rootURL')#/reset_password.cfm?activate=#arguments.activate#&outcome=failure&#local.error#&email=#arguments.email#" addToken="no" />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="terminateUser" access="public" output="true" returntype="void">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfset $.run.it('craftivitySErvice.terminateUser',{userID=arguments.userID}) />
		
		<cflocation url="#$.site.getVar('rootURL')#/craftivity/user_management.cfm" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="signupBetaAccess" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.message = "" />
		
		<cfquery name="local.checkEmail" datasource="#$.site.getVar('datasource')#">
			select 
			email,
			created
			from signup_email
			where active = 1
			and email = '#arguments.email#'
		</cfquery>
		
		<cfif local.checkEmail.recordcount gt 0>
			<cfset local.message = "message=Your email was already in the queue, you'll hear from us shortly." />
		<cfelse>
			<cfset $.database.save(
				table="signup_email",
				id="",
				email=arguments.email		
			) />
			<cfset local.message = "message=Thanks, you'll be hearing from us shortly!" />
		</cfif>
				
		<cflocation url="#$.site.getVar('rootURL')#/beta_signup.cfm?#local.message#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="access" access="public" output="true" returntype="void">
		<cfargument name="ase" required="false" default="" />
		<cfargument name="asdf" required="false" default="" />
		<cfargument name="bsh" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfset local.url = "#$.site.getVar('rootURL')#/craftivity/access.cfm?" />
		<cfif arguments.ase eq "hello1kitty">
			<cfset local.url = $.link.setArg(local.url,'ase',arguments.ase) />
			<cfset local.url = $.link.setArg(local.url,'bsh',arguments.bsh) />
			<cfset local.url = $.link.setArg(local.url,'va125b',"true") />
			<cfset local.url = $.link.setArg(local.url,'asdf',arguments.asdf) />
		</cfif>
				
		<cflocation url="#local.url#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendBetaEmails" access="public" output="true" returntype="void">
		
		<cfset var local = {} />
		
		
		<cfset $.run.it('craftivityService.sendBetaEmails') />
				
		<cflocation url="#cgi.http_referer#" addToken="no" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="unsubscribe" access="public" output="true" returntype="void">
		<cfargument name="email" required="False" default="" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.run.it('craftivityService.getUser',{email=arguments.email}) />
		<cfif local.user.recordcount gt 0 and local.user.recordcount lt 5>
			<cfset $.database.save(
				id=local.user.id,
				table="user",
				unsubscribe=1
			) />
			<cfset local.outcome = 'success' />
		<cfelse>
			<cfset local.outcome = 'nouser' />
		</cfif>
		
		<cflocation url="#$.site.getVar('rootURL')#/unsubscribe.cfm?outcome=#local.outcome#&email=#arguments.email#" addToken="no" />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>